package com.xiaoluyy.demo.common.util;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ValidateRange {
    public static boolean validateRange(Object obj , double min,double max)
    {
        if(obj instanceof String){
            if(obj.toString().length()<min||obj.toString().length()>max){
                return false;
            }
        }else if (obj instanceof Integer){
            if((Integer)obj < min||(Integer)obj > max){
                return false;
            }
        }else if(obj instanceof Double){
            if((Double)obj < min||(Double)obj > max){
                return false;
            }
        }else if(obj instanceof Long){
            if((Long)obj < min||(Long)obj > max){
                return false;
            }
        }
        return true;
    }

    public static void validateRange(Object obj,boolean isNotNull,String name,double min,double max)throws IllegalArgumentException
    {
        String nullMsg=name+"不能为空！";
        String boundsMsg=name+"取值越界！最小"+min+"，最大"+max+"。";

        try {
            if(obj instanceof String){
                boundsMsg=name+"取值越界！长度最少"+min+"个字符，最多"+max+"个字符。";
                if (isNotNull&&(obj==null || obj.toString().length()==0)){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(obj.toString().length()<min||obj.toString().length() >max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if (obj instanceof Integer){
                if(isNotNull&&(obj==null || (Integer)obj ==0 )){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Integer)obj!=0&&((Integer)obj < min||(Integer)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if(obj instanceof Double){
                if(isNotNull&&(obj==null || (Double)obj ==0 )){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Double)obj!=0&&((Double)obj < min||(Double)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if(obj instanceof Long){
                if(isNotNull&&(obj==null || (Long)obj ==0 )){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Long)obj!=0&&((Long)obj < min||(Long)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else{
                boundsMsg=name+"取值越界！长度最少"+min+"个，最多"+max+"个。";
                if(isNotNull&&obj==null){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(obj.toString().length()<min||obj.toString().length() >max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }
        }catch (ClassCastException e){
            throw new IllegalArgumentException(boundsMsg);
        }

    }
    public static void validateRange(Object obj,boolean isNotNull,String name,int min,int max)throws IllegalArgumentException
    {
        String nullMsg=name+"不能为空！";
        String boundsMsg=name+"取值越界！最小"+min+"，最大"+max+"。";
        try {
            if(obj instanceof String){
                boundsMsg=name+"取值越界！长度最少"+min+"个字符，最多"+max+"个字符。";
                if (isNotNull&&obj.toString().length()==0){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(obj.toString().length()<min||obj.toString().length() >max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if (obj instanceof Integer){
                if(isNotNull&&(Integer)obj ==0 ){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Integer)obj!=0&&((Integer)obj < min||(Integer)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if (obj instanceof Short){
                if(isNotNull&&(Short)obj ==0 ){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Short)obj!=0&&((Short)obj < min||(Short)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if(obj instanceof Double){
                if(isNotNull&&(Double)obj ==0 ){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Double)obj!=0&&((Double)obj < min||(Double)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else if(obj instanceof Long){
                if(isNotNull&& (Long)obj ==0 ){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(Long)obj!=0&&((Long)obj < min||(Long)obj > max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }else{
                boundsMsg=name+"取值越界！长度最少"+min+"个，最多"+max+"个。";
                if(isNotNull&&obj==null){
                    throw new IllegalArgumentException(nullMsg);
                }
                if(obj!=null&&(obj.toString().length()<min||obj.toString().length() >max)){
                    throw new IllegalArgumentException(boundsMsg);
                }
            }
        }catch (ClassCastException e){
            throw new IllegalArgumentException(boundsMsg);
        }

    }


    public static void validateRangeException(Object obj , double min,double max,String msg)throws IllegalArgumentException
    {
        if (!validateRange(obj,min,max)){
            throw new IllegalArgumentException(msg);
        }

    }


    public static void validateRangeException(Object obj) throws IllegalArgumentException {
        Class tClass = obj.getClass();
        //获得该类的所有属性
        Field[] fields = tClass.getDeclaredFields();
        try {
            for(Field field:fields){
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), tClass);
                //获得get方法
                Method get = pd.getReadMethod();
                if (get.isAnnotationPresent(ValidatRange.class)) {
                    // iterates all the annotations available in the method
                    for (Annotation anno : get.getDeclaredAnnotations()) {
                        System.out.println("Annotation in Method '"+ get + "' : " + anno);
                    }
                    ValidatRange methodAnno = get.getAnnotation(ValidatRange.class);
                    Object getValue = get.invoke(obj, new Object[]{});
                    if(methodAnno.type().equals("String")){
                        if(methodAnno.isNull()&&(getValue==null||getValue.toString().isEmpty())){
                            throw new IllegalArgumentException(methodAnno.emptyErrorMsg());
                        }
                        if(obj.toString().length()<methodAnno.min()||obj.toString().length() >methodAnno.max()){
                            throw new IllegalArgumentException(methodAnno.rangeErrorMsg());
                        }
                    }else if(methodAnno.type().equals("int")){
                        if(methodAnno.isNull()&&(Integer)getValue==0){
                            throw new IllegalArgumentException(methodAnno.emptyErrorMsg());
                        }
                        if((Integer)getValue<methodAnno.min()||(Integer)getValue>methodAnno.max()){
                            throw new IllegalArgumentException(methodAnno.rangeErrorMsg());
                        }
                    }else if(methodAnno.type().equals("double")){
                        if(methodAnno.isNull()&&(Double)getValue==0){
                            throw new IllegalArgumentException(methodAnno.emptyErrorMsg());
                        }
                        if((Double)getValue<methodAnno.min()||(Double)getValue>methodAnno.max()){
                            throw new IllegalArgumentException(methodAnno.rangeErrorMsg());
                        }
                    }else if(methodAnno.type().equals("long")){
                        if(methodAnno.isNull()&&(Long)getValue==0){
                            throw new IllegalArgumentException(methodAnno.emptyErrorMsg());
                        }
                        if((Long)getValue<methodAnno.min()||(Long)getValue>methodAnno.max()){
                            throw new IllegalArgumentException(methodAnno.rangeErrorMsg());
                        }
                    }
                }
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }

    public static  int getWordCountRegex(String s)
    {
        s = s.replaceAll("[^\\x00-\\xff]", "**");
        int length = s.length();
        return length;
    }

    public static  int getWordCountRegex(Object s)
    {
        s = s.toString().replaceAll("[^\\x00-\\xff]", "**");
        int length = s.toString().length();
        return length;
    }


}
